package com.zj.ssm.service.impl;

import com.zj.ssm.mapper.UdepartMapper;
import com.zj.ssm.pojo.domain.*;
import com.zj.ssm.pojo.domain.vo.*;
import com.zj.ssm.result.AjaxResult;
import com.zj.ssm.service.UdepartService;
import com.zj.ssm.sysconst.LoginConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author lilinhui
 * @author diao 2022/2/20 星期日 13:53
 */
@Service
public class UdepartServiceImpl implements UdepartService {


    @Autowired
    private UdepartMapper udepartMapper;

    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult list() {

        List<Hosd>  list = udepartMapper.list();
        session.setAttribute(LoginConst.KESHI_IN_SESSION,list);
        if (list != null) {
            return AjaxResult.success(list);
        }
        return AjaxResult.fail("失败");
    }

    @Override
    public AjaxResult departId(Integer id){

        session.setAttribute(LoginConst.KESHI_IN_SESSION,id);
        DeparVo deparVo = new DeparVo();
        Date d = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(d);
        deparVo.setId(id);
        deparVo.setDate(date);
        List<Udoctor> udoctors = udepartMapper.departId(deparVo);
        if (udoctors ==null){
            return AjaxResult.fail("失败");
        }
        return AjaxResult.success(udoctors);
    }

    @Override
    public AjaxResult doctor(Integer yid) {
        session.setAttribute(LoginConst.KESHI_IN_DOCTOR,yid);
        ListDoctor listDoctor = udepartMapper.ListDoctors(yid);
        Integer price = listDoctor.getPrice();

        session.setAttribute(LoginConst.KESHI_IN_PRICE,price);
        if (listDoctor != null){
            return AjaxResult.success(listDoctor);
        }

        return AjaxResult.fail();
    }

    @Override
    public AjaxResult lost(UdoctorVo udoctorVo) {

        Integer o = (Integer) session.getAttribute(LoginConst.KESHI_IN_DOCTOR);
        udoctorVo.setYid(o);
        Integer yid = udoctorVo.getYid();
        int lost = udepartMapper.lost(yid);

        if (lost>0){
            return AjaxResult.success();
        }

        return AjaxResult.fail("挂号失败");
    }

    @Override
    public AjaxResult subscribe(SubscribeVo subscribeVo) {

        Integer a = (Integer) session.getAttribute(LoginConst.KESHI_IN_SESSION);
        Integer b = (Integer) session.getAttribute(LoginConst.KESHI_IN_DOCTOR);
        subscribeVo.setKid(a);
        subscribeVo.setYid(b);

        int i = udepartMapper.subscribe(subscribeVo);

        if (i>0){
            return AjaxResult.success();
        }

        return AjaxResult.fail("预约失败");
    }

    @Override
    public AjaxResult information(String name, String identity) {

         List<Query> query = udepartMapper. query(name, identity);


        return AjaxResult.success(query);

    }

    @Override
    public AjaxResult patResult(Integer id) {

        PatResults patResults = udepartMapper.patResult(id);
        if (patResults != null){
            return AjaxResult.success(patResults);
        }

        return AjaxResult.fail();
    }

    @Override
    public AjaxResult abolish(String name, String identity) {

        int abolish = udepartMapper.abolish(name, identity);
        if (abolish>0){
            return AjaxResult.success();
        }

        return AjaxResult.fail("取消预约失败");
    }


}
